brainlang <- read.csv("https://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter16/chap16q15LanguageGreyMatter.csv")
str(brainlang)
## 'data.frame': 22 obs. of 2 variables:
## $ proficiency: num 0.26 0.44 0.89 1.26 1.69 1.97 1.98 2.24 2.24 2.58 ...
## $ greymatter : num -0.07 -0.08 -0.008 -0.009 -0.023 -0.009 -0.036 -0.029 -0.008 -0.023 ...
summary(brainlang)
## proficiency greymatter
## Min. :0.260 Min. :-0.0800000
## 1st Qu.:1.972 1st Qu.:-0.0195000
## Median :2.525 Median :-0.0070000
## Mean :2.400 Mean : 0.0004091
## 3rd Qu.:3.163 3rd Qu.: 0.0227500
## Max. :3.850 Max. : 0.0620000
# vars are proficiency (treatment) and grey matter (outcome)
library(ggplot2)
library(ggpubr)
brainlang_basicviz <- ggplot(data = brainlang,
mapping = aes(x = proficiency,
y = greymatter)) +
geom_point()
brainlang_basicviz
# Adding a more advanced plot that includes R2 value and pearson correlation
brainlang_advancedviz <- ggscatter(brainlang,
x = "proficiency",
y = "greymatter",
add = "reg.line",
conf.int = TRUE,
cor.coef = TRUE,
cor.method = "pearson") +
labs(title = "Does learning a second language change brain structure?",
subtitle = "Comparing second language proficiency on grey matter volume",
x = "Second language proficiency rating",
y = "Grey matter volume")
brainlang_advancedviz
## `geom_smooth()` using formula 'y ~ x'
cor(brainlang$greymatter,
brainlang$proficiency)
## [1] 0.8183134
library(performance)
brainlang_lm <- lm(greymatter ~ proficiency,
data = brainlang)
check_model(brainlang_lm)
# Posterior Predictive Check - simulated data compared to the observed data
# Linearity - observation depends on the treatment/condition
# Homogeneity of variance - variances of two or more samples are considered equal
# Independence of errors / Influential Observations - there is no relationship between the errors and the observations
# Normality (of residuals) - treatment follows a normal (Gaussian) distribution
# Predictions check
check_predictions(brainlang_lm,
iterations = 100)
# Predicted lines do not closely fit the observed data
# Linearity check
library(modelr)
##
## Attaching package: 'modelr'
## The following objects are masked from 'package:performance':
##
## mae, mse, rmse
brainlang <- brainlang |>
add_predictions(brainlang_lm) |>
add_residuals(brainlang_lm)
ggplot(brainlang,
mapping = aes(x = pred, y = resid)) +
geom_point() +
stat_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# The reference line is not flat / horizontal - data appears non-linear
# Alternative plotting method:
# check_model(brainlang_lm, check = "ncv") %>%
# plot()
# Homogeneity:
check_heteroscedasticity(brainlang_lm) |>
plot()
# again, data appears non-linear
# Normality:
check_normality(brainlang_lm) %>%
plot()
# Distribution is close-ish but not that close to the normal curve
# Alternative plotting method:
# check_normality(brainlang_lm) %>%
# plot(type = "qq")
# Outliers:
plot(brainlang_lm, which = 4)
# distance is larger than previous examples we have covered
check_outliers(brainlang_lm) %>%
plot()
# All points are within the contour lines though
1. Looking at the plots in 1a, the general trend is that as second language profic. increases, grey matter volume increases.
2. I'll check the R2 value to see how closely the independent variable is related to the dependent variable
model_performance(brainlang_lm)
## # Indices of model performance
##
## AIC | BIC | R2 | R2 (adj.) | RMSE | Sigma
## ------------------------------------------------------
## -101.941 | -98.668 | 0.670 | 0.653 | 0.021 | 0.022
1. R2 value = 0.67. Somewhat strong correlation suggesting that language proficiency does increase grey matter
grass <- read.csv("https://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter17/chap17q19GrasslandNutrientsPlantSpecies.csv")
str(grass)
## 'data.frame': 10 obs. of 2 variables:
## $ nutrients: int 0 0 0 1 2 3 1 3 4 4
## $ species : int 36 36 32 34 33 30 20 23 21 16
summary(grass)
## nutrients species
## Min. :0.00 Min. :16.00
## 1st Qu.:0.25 1st Qu.:21.50
## Median :1.50 Median :31.00
## Mean :1.80 Mean :28.10
## 3rd Qu.:3.00 3rd Qu.:33.75
## Max. :4.00 Max. :36.00
library(ggplot2)
grass_basicviz <- ggplot(data = grass,
mapping = aes(x = nutrients,
y = species)) +
geom_point()
grass_basicviz
# Create model
grass_lm <- lm(species ~ nutrients,
data = grass)
# Check it's fit
check_model(grass_lm)
# Check the estimate (rate of change) of the fitted line
library(broom)
##
## Attaching package: 'broom'
## The following object is masked from 'package:modelr':
##
## bootstrap
tidy(grass_lm) # estimate = -3.34
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 34.1 2.60 13.1 0.00000108
## 2 nutrients -3.34 1.10 -3.04 0.0161
library(ggpmisc)
## Loading required package: ggpp
##
## Attaching package: 'ggpp'
## The following object is masked from 'package:ggplot2':
##
## annotate
grass_fit_plot <- ggplot(data = grass_lm,
mapping = aes(x = nutrients,
y = species)) +
stat_poly_line() +
stat_poly_eq(aes(label = paste(after_stat(eq.label),
after_stat(rr.label), sep = "*\", \"*"))) +
geom_point() +
stat_smooth(method = "lm",
formula = y ~ x)
grass_fit_plot
### R2 = 0.54
1. treatment = nutrients = x
2. outcome = species = y
3. Math:
a. f(x) = 34.1 - 3.34x
b. f(0) = 34.1
beetle <- read.csv("https://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter17/chap17q25BeetleWingsAndHorns.csv")
str(beetle)
## 'data.frame': 19 obs. of 2 variables:
## $ hornSize: num 0.074 0.079 0.019 0.017 0.085 0.081 0.011 0.023 0.005 0.007 ...
## $ wingMass: num -42.8 -21.7 -18.8 -16 -12.8 11.6 7.6 1.6 3.7 1.1 ...
summary(beetle)
## hornSize wingMass
## Min. :-0.17700 Min. :-42.800
## 1st Qu.:-0.03950 1st Qu.: -7.850
## Median : 0.00500 Median : 3.700
## Mean :-0.01089 Mean : 1.316
## 3rd Qu.: 0.02100 3rd Qu.: 12.300
## Max. : 0.08500 Max. : 22.200
beetle_basicviz <- ggplot(data = beetle,
mapping = aes(x = hornSize,
y = wingMass)) +
geom_point()
beetle_basicviz
beetle_lm <- lm(wingMass ~ hornSize,
data = beetle)
beetle_lm
##
## Call:
## lm(formula = wingMass ~ hornSize, data = beetle)
##
## Coefficients:
## (Intercept) hornSize
## -0.129 -132.617
summary(beetle_lm)
##
## Call:
## lm(formula = wingMass ~ hornSize, data = beetle)
##
## Residuals:
## Min 1Q Median 3Q Max
## -32.857 -8.715 2.157 6.984 22.471
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.129 3.355 -0.038 0.9698
## hornSize -132.617 45.146 -2.937 0.0092 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 14.47 on 17 degrees of freedom
## Multiple R-squared: 0.3367, Adjusted R-squared: 0.2977
## F-statistic: 8.629 on 1 and 17 DF, p-value: 0.009201
beetle <- beetle %>%
add_predictions(beetle_lm) %>%
add_residuals(beetle_lm)
beetle_residual_plot <- ggplot(data = beetle,
mapping = aes(x = pred,
y = resid)) +
geom_point() +
stat_smooth()
beetle_residual_plot
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
check_model(beetle_lm)
# linearity and homogeneity of variance look weird, PPC maybe looks weird
# looking at homogeneity of variance closer
check_heteroskedasticity(beetle_lm) %>%
plot()
# looking at linearity closer
check_model(beetle_lm, check = "ncv") %>%
plot()
# this could be non-linearity
1. Transformation for non-linearity by log transformation
check_normality(beetle_lm) %>%
plot()
# distribution does not really fit the normal curve too closely
plot(beetle_lm, which = 4)
check_outliers(beetle_lm) %>%
plot(type = "bar")
check_outliers(beetle_lm) %>%
plot()
# there are a few data points (1, 6, 19) that are on the cusp of breaking the contour lines
teeth <- read.csv("https://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter17/chap17q30NuclearTeeth.csv")
str(teeth)
## 'data.frame': 16 obs. of 2 variables:
## $ dateOfBirth: num 1986 1984 1990 1988 1990 ...
## $ deltaC14 : int 89 109 91 127 99 110 123 105 622 262 ...
summary(teeth)
## dateOfBirth deltaC14
## Min. :1964 Min. : 89.0
## 1st Qu.:1972 1st Qu.:108.0
## Median :1984 Median :125.0
## Mean :1980 Mean :237.4
## 3rd Qu.:1988 3rd Qu.:370.0
## Max. :1990 Max. :622.0
teeth_basicviz <- ggplot(data = teeth,
mapping = aes(x = deltaC14,
y = dateOfBirth)) +
geom_point()
teeth_basicviz
teeth_lm <- lm(dateOfBirth ~ deltaC14,
data = teeth)
teeth_fit_plot <- ggplot(data = teeth_lm,
mapping = aes(x = deltaC14,
y = dateOfBirth)) +
stat_poly_line() +
stat_poly_eq(aes(label = paste(after_stat(eq.label),
after_stat(rr.label), sep = "*\", \"*"))) +
geom_point() +
stat_smooth(method = "lm",
formula = y ~ x)
teeth_fit_plot
# slope `is -0.0533
1. The inner pair of dotted lines show confidence bands
2. These indicate the range that the population parameter is likely to fall within
1. The outer pair of dotted lines show the prediction interval
2. These indicate the range that likely contains a new value for a dependent variable given a new observation
teeth_model_log <- lm(log(dateOfBirth) ~ deltaC14,
data = teeth)
x_frame <- teeth %>%
data_grid(deltaC14 = seq_range(deltaC14, n = 100))
teeth_log_curve <- data_grid(teeth,
deltaC14 = seq_range(deltaC14, n = 100)) %>%
augment(x = teeth_model_log,
data = .x,
interval = "confidence") %>%
mutate(across(c(.fitted:.upper),
.fns = exp))
teeth_log_curve
## # A tibble: 100 × 4
## deltaC14 .fitted .lower .upper
## <dbl> <dbl> <dbl> <dbl>
## 1 89 1988. 1985. 1990.
## 2 94.4 1987. 1985. 1990.
## 3 99.8 1987. 1985. 1989.
## 4 105. 1987. 1984. 1989.
## 5 111. 1986. 1984. 1989.
## 6 116. 1986. 1984. 1988.
## 7 121. 1986. 1984. 1988.
## 8 127. 1986. 1983. 1988.
## 9 132. 1985. 1983. 1987.
## 10 137. 1985. 1983. 1987.
## # … with 90 more rows
teeth_pred <- augment(x = teeth_lm,
newdata = x_frame,
interval = "prediction")
teeth_pred
## # A tibble: 100 × 4
## deltaC14 .fitted .lower .upper
## <dbl> <dbl> <dbl> <dbl>
## 1 89 1988. 1980. 1995.
## 2 94.4 1987. 1980. 1995.
## 3 99.8 1987. 1979. 1994.
## 4 105. 1987. 1979. 1994.
## 5 111. 1986. 1979. 1994.
## 6 116. 1986. 1979. 1994.
## 7 121. 1986. 1978. 1993.
## 8 127. 1986. 1978. 1993.
## 9 132. 1985. 1978. 1993.
## 10 137. 1985. 1978. 1992.
## # … with 90 more rows
teeth_conf_pred_plot <- teeth_basicviz +
geom_line(data = teeth_log_curve,
mapping = aes(y = .fitted)) +
geom_ribbon(data = teeth_log_curve,
mapping = aes(y = .fitted,
ymin = .lower,
ymax =.upper),
alpha = 0.2) +
geom_line(data = teeth_pred,
mapping = aes(y = .fitted)) +
geom_ribbon(data = teeth_pred,
mapping = aes(y = .fitted,
ymin = .lower,
ymax =.upper),
alpha = 0.2)
teeth_conf_pred_plot
mosquito <- read.csv("/Users/nathanstrozewski/Documents/Everything/Education/M.S. Biology UMB/Courses/003_F2022/Biological Data Analysis/Homework/Homework #5/Data/17q24DEETMosquiteBites.csv")
mosquito_model <- lm(bites ~ dose,
data = mosquito)
## Look at vcov()
vcov(mosquito_model)
## (Intercept) dose
## (Intercept) 0.09929780 -0.025986850
## dose -0.02598685 0.007437073
## Look at mnormt()
library(mnormt)
rmnorm(4, mean = coef(mosquito_model), varcov = vcov(mosquito_model))
## (Intercept) dose
## [1,] 4.266033 -0.4929546
## [2,] 3.753990 -0.3949349
## [3,] 4.255887 -0.4820478
## [4,] 3.663541 -0.3276644
1. Using geom_abline(), make a plot that has the following layers:
a. the data
b. the lm fit with a CI, and
c. simulated lines
mosquito_plot <- ggplot(data = mosquito,
mapping = aes(x = dose,
y = bites)) +
geom_point()
mosquito_plot
mosquito_model_log <- lm(log(bites) ~ dose, data = mosquito)
mosquito_log_curve <- data_grid(mosquito,
dose = seq_range(dose, n = 100)) %>%
augment(x = mosquito_model_log,
data = .x,
interval = "confidence") %>%
mutate(across(c(.fitted:.upper),
.fns = exp))
mosquito_log_curve
## # A tibble: 100 × 4
## dose .fitted .lower .upper
## <dbl> <dbl> <dbl> <dbl>
## 1 1.4 3.40 2.81 4.11
## 2 1.45 3.37 2.80 4.07
## 3 1.49 3.34 2.78 4.02
## 4 1.54 3.32 2.77 3.97
## 5 1.59 3.29 2.76 3.92
## 6 1.63 3.26 2.74 3.88
## 7 1.68 3.23 2.73 3.83
## 8 1.73 3.20 2.71 3.79
## 9 1.77 3.18 2.70 3.74
## 10 1.82 3.15 2.68 3.70
## # … with 90 more rows
mosquito_sims <- data.frame(rmnorm(100, mean = coef(mosquito_model), varcov = vcov(mosquito_model)))
mosquito_sims
## X.Intercept. dose
## 1 4.179555 -0.5010676
## 2 3.944414 -0.4757730
## 3 3.346060 -0.3172197
## 4 4.065938 -0.4743009
## 5 3.947823 -0.3941353
## 6 4.148586 -0.5125126
## 7 3.972883 -0.4251264
## 8 4.259998 -0.4934171
## 9 4.469265 -0.5288779
## 10 3.829872 -0.4074737
## 11 4.334294 -0.4965038
## 12 3.710554 -0.3820394
## 13 3.559839 -0.3427657
## 14 4.219150 -0.4847547
## 15 4.009487 -0.4672119
## 16 3.806797 -0.4031999
## 17 3.975499 -0.3826295
## 18 3.831982 -0.4069850
## 19 3.789073 -0.3490203
## 20 3.750611 -0.4024477
## 21 3.315931 -0.2564231
## 22 4.407929 -0.5280831
## 23 4.377303 -0.5528935
## 24 3.658381 -0.3697326
## 25 4.396758 -0.5897488
## 26 3.734986 -0.3731768
## 27 3.703550 -0.3328067
## 28 3.755076 -0.4171308
## 29 3.236346 -0.2281632
## 30 3.656518 -0.3418796
## 31 3.781250 -0.3770837
## 32 3.955604 -0.4288560
## 33 4.284029 -0.5286386
## 34 3.194729 -0.2278994
## 35 3.020334 -0.1821160
## 36 3.706582 -0.3738850
## 37 3.615785 -0.3635896
## 38 3.766586 -0.3759920
## 39 3.953232 -0.4345951
## 40 3.780146 -0.3272326
## 41 3.732774 -0.3418665
## 42 4.198520 -0.5096941
## 43 4.134507 -0.4512259
## 44 3.910132 -0.4328051
## 45 4.104486 -0.4619389
## 46 4.410975 -0.5491797
## 47 4.186417 -0.4965248
## 48 4.107593 -0.4625967
## 49 3.710886 -0.3807319
## 50 4.139040 -0.4562042
## 51 4.337959 -0.5462528
## 52 4.292496 -0.5773790
## 53 4.122338 -0.4753490
## 54 4.203322 -0.5045451
## 55 4.512315 -0.5565402
## 56 3.957226 -0.4213563
## 57 3.862381 -0.3798935
## 58 4.103151 -0.4388768
## 59 3.894105 -0.4084257
## 60 3.621001 -0.3522379
## 61 3.686992 -0.3498389
## 62 3.510224 -0.3206722
## 63 3.565779 -0.3366080
## 64 3.848933 -0.3678828
## 65 3.568410 -0.3535435
## 66 3.559730 -0.3207468
## 67 3.739270 -0.3951595
## 68 4.573360 -0.6516120
## 69 3.676290 -0.2937165
## 70 3.685993 -0.3851631
## 71 4.086000 -0.4743703
## 72 4.505385 -0.5617108
## 73 4.583815 -0.5634594
## 74 3.690767 -0.3437096
## 75 3.927508 -0.4197293
## 76 3.879901 -0.4059771
## 77 4.087428 -0.4870025
## 78 3.688273 -0.3843254
## 79 3.597398 -0.3323570
## 80 4.160061 -0.4278735
## 81 4.086790 -0.4743768
## 82 3.746937 -0.3664268
## 83 3.785830 -0.3541823
## 84 4.053035 -0.4498136
## 85 3.337257 -0.2887042
## 86 4.320307 -0.5804430
## 87 3.893212 -0.4115677
## 88 3.453204 -0.3235862
## 89 3.521160 -0.2769960
## 90 3.886727 -0.4218223
## 91 4.122523 -0.4844351
## 92 3.920514 -0.3928466
## 93 4.005262 -0.4685748
## 94 3.714949 -0.3199666
## 95 4.028866 -0.4591009
## 96 3.815141 -0.3738136
## 97 3.523394 -0.3003050
## 98 3.759121 -0.3911920
## 99 3.917464 -0.4252375
## 100 3.952978 -0.3944163
mosquito_plot_5b <- mosquito_plot +
geom_line(data = mosquito_log_curve,
mapping = aes(y = .fitted)) +
geom_ribbon(data = mosquito_log_curve,
mapping = aes(y = .fitted,
ymin = .lower,
ymax =.upper),
alpha = 0.5) +
geom_abline(slope = mosquito_sims$dose,
intercept = mosquito_sims$X.Intercept)
mosquito_plot_5b
summary(mosquito_model_log)
##
## Call:
## lm(formula = log(bites) ~ dose, data = mosquito)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.72605 -0.16398 0.01497 0.20449 0.47065
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.48157 0.14695 10.082 1.22e-13 ***
## dose -0.18368 0.04022 -4.567 3.25e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3099 on 50 degrees of freedom
## Multiple R-squared: 0.2944, Adjusted R-squared: 0.2803
## F-statistic: 20.86 on 1 and 50 DF, p-value: 3.25e-05
mosquito_predictions <- data.frame(predict(mosquito_model_log, newdata = mosquito_sims, interval = "predict"))
mosquito_predictions
## fit lwr upr
## 1 1.573610 0.8671219 2.280099
## 2 1.568964 0.8634068 2.274522
## 3 1.539841 0.8400116 2.239670
## 4 1.568694 0.8631904 2.274197
## 5 1.553969 0.8513841 2.256554
## 6 1.575713 0.8688013 2.282624
## 7 1.559661 0.8559539 2.263369
## 8 1.572205 0.8659987 2.278412
## 9 1.578719 0.8712011 2.286236
## 10 1.556419 0.8533518 2.259486
## 11 1.572772 0.8664519 2.279092
## 12 1.551747 0.8495986 2.253896
## 13 1.544533 0.8437937 2.245273
## 14 1.570614 0.8647265 2.276502
## 15 1.567392 0.8621483 2.272635
## 16 1.555634 0.8527215 2.258546
## 17 1.551855 0.8496857 2.254025
## 18 1.556329 0.8532798 2.259379
## 19 1.545682 0.8447189 2.246645
## 20 1.555496 0.8526105 2.258381
## 21 1.528674 0.8309909 2.226356
## 22 1.578573 0.8710846 2.286061
## 23 1.583130 0.8747192 2.291541
## 24 1.549487 0.8477808 2.251192
## 25 1.589900 0.8801101 2.299689
## 26 1.550119 0.8482896 2.251949
## 27 1.542704 0.8423198 2.243088
## 28 1.558193 0.8547756 2.261610
## 29 1.523483 0.8267884 2.220177
## 30 1.544370 0.8436626 2.245078
## 31 1.550837 0.8488667 2.252807
## 32 1.560346 0.8565034 2.264190
## 33 1.578675 0.8711661 2.286184
## 34 1.523434 0.8267491 2.220119
## 35 1.515025 0.8199276 2.210122
## 36 1.550249 0.8483943 2.252104
## 37 1.548358 0.8468730 2.249843
## 38 1.550636 0.8487055 2.252567
## 39 1.561401 0.8573487 2.265453
## 40 1.541680 0.8414946 2.241865
## 41 1.544368 0.8436606 2.245075
## 42 1.575195 0.8683878 2.282002
## 43 1.564455 0.8597969 2.269114
## 44 1.561072 0.8570851 2.265059
## 45 1.566423 0.8613729 2.271474
## 46 1.582448 0.8741755 2.290720
## 47 1.572776 0.8664550 2.279097
## 48 1.566544 0.8614696 2.271618
## 49 1.551507 0.8494055 2.253608
## 50 1.565370 0.8605294 2.270210
## 51 1.581910 0.8737468 2.290074
## 52 1.587628 0.8783018 2.296953
## 53 1.568886 0.8633444 2.274428
## 54 1.574249 0.8676323 2.280866
## 55 1.583800 0.8752531 2.292347
## 56 1.558969 0.8553984 2.262539
## 57 1.551353 0.8492817 2.253424
## 58 1.562187 0.8579792 2.266395
## 59 1.556594 0.8534922 2.259695
## 60 1.546273 0.8451948 2.247351
## 61 1.545832 0.8448400 2.246825
## 62 1.540475 0.8405230 2.240427
## 63 1.543402 0.8428825 2.243922
## 64 1.549147 0.8475075 2.250786
## 65 1.546513 0.8453879 2.247638
## 66 1.540489 0.8405341 2.240443
## 67 1.554157 0.8515353 2.256779
## 68 1.601263 0.8891370 2.313389
## 69 1.535524 0.8365276 2.234520
## 70 1.552321 0.8500598 2.254582
## 71 1.568707 0.8632006 2.274213
## 72 1.584750 0.8760098 2.293489
## 73 1.585071 0.8762657 2.293876
## 74 1.544707 0.8439333 2.245480
## 75 1.558670 0.8551586 2.262182
## 76 1.556144 0.8531311 2.259157
## 77 1.571027 0.8650567 2.276997
## 78 1.552167 0.8499361 2.254398
## 79 1.542621 0.8422533 2.242989
## 80 1.560166 0.8563587 2.263973
## 81 1.568708 0.8632016 2.274214
## 82 1.548879 0.8472923 2.250466
## 83 1.546630 0.8454823 2.247778
## 84 1.564196 0.8595891 2.268803
## 85 1.534603 0.8357841 2.233422
## 86 1.588190 0.8787499 2.297631
## 87 1.557171 0.8539555 2.260386
## 88 1.541010 0.8409546 2.241066
## 89 1.532452 0.8340466 2.230858
## 90 1.559055 0.8554671 2.262642
## 91 1.570555 0.8646795 2.276431
## 92 1.553732 0.8511940 2.256270
## 93 1.567642 0.8623487 2.272936
## 94 1.540345 0.8404185 2.240272
## 95 1.565902 0.8609555 2.270848
## 96 1.550236 0.8483837 2.252089
## 97 1.536734 0.8375047 2.235963
## 98 1.553428 0.8509498 2.255907
## 99 1.559682 0.8559703 2.263393
## 100 1.554020 0.8514256 2.256615
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mosquito_predictions_withbounds <- cbind(mosquito_sims, mosquito_predictions)
mosquito_predictions_withbounds
## X.Intercept. dose fit lwr upr
## 1 4.179555 -0.5010676 1.573610 0.8671219 2.280099
## 2 3.944414 -0.4757730 1.568964 0.8634068 2.274522
## 3 3.346060 -0.3172197 1.539841 0.8400116 2.239670
## 4 4.065938 -0.4743009 1.568694 0.8631904 2.274197
## 5 3.947823 -0.3941353 1.553969 0.8513841 2.256554
## 6 4.148586 -0.5125126 1.575713 0.8688013 2.282624
## 7 3.972883 -0.4251264 1.559661 0.8559539 2.263369
## 8 4.259998 -0.4934171 1.572205 0.8659987 2.278412
## 9 4.469265 -0.5288779 1.578719 0.8712011 2.286236
## 10 3.829872 -0.4074737 1.556419 0.8533518 2.259486
## 11 4.334294 -0.4965038 1.572772 0.8664519 2.279092
## 12 3.710554 -0.3820394 1.551747 0.8495986 2.253896
## 13 3.559839 -0.3427657 1.544533 0.8437937 2.245273
## 14 4.219150 -0.4847547 1.570614 0.8647265 2.276502
## 15 4.009487 -0.4672119 1.567392 0.8621483 2.272635
## 16 3.806797 -0.4031999 1.555634 0.8527215 2.258546
## 17 3.975499 -0.3826295 1.551855 0.8496857 2.254025
## 18 3.831982 -0.4069850 1.556329 0.8532798 2.259379
## 19 3.789073 -0.3490203 1.545682 0.8447189 2.246645
## 20 3.750611 -0.4024477 1.555496 0.8526105 2.258381
## 21 3.315931 -0.2564231 1.528674 0.8309909 2.226356
## 22 4.407929 -0.5280831 1.578573 0.8710846 2.286061
## 23 4.377303 -0.5528935 1.583130 0.8747192 2.291541
## 24 3.658381 -0.3697326 1.549487 0.8477808 2.251192
## 25 4.396758 -0.5897488 1.589900 0.8801101 2.299689
## 26 3.734986 -0.3731768 1.550119 0.8482896 2.251949
## 27 3.703550 -0.3328067 1.542704 0.8423198 2.243088
## 28 3.755076 -0.4171308 1.558193 0.8547756 2.261610
## 29 3.236346 -0.2281632 1.523483 0.8267884 2.220177
## 30 3.656518 -0.3418796 1.544370 0.8436626 2.245078
## 31 3.781250 -0.3770837 1.550837 0.8488667 2.252807
## 32 3.955604 -0.4288560 1.560346 0.8565034 2.264190
## 33 4.284029 -0.5286386 1.578675 0.8711661 2.286184
## 34 3.194729 -0.2278994 1.523434 0.8267491 2.220119
## 35 3.020334 -0.1821160 1.515025 0.8199276 2.210122
## 36 3.706582 -0.3738850 1.550249 0.8483943 2.252104
## 37 3.615785 -0.3635896 1.548358 0.8468730 2.249843
## 38 3.766586 -0.3759920 1.550636 0.8487055 2.252567
## 39 3.953232 -0.4345951 1.561401 0.8573487 2.265453
## 40 3.780146 -0.3272326 1.541680 0.8414946 2.241865
## 41 3.732774 -0.3418665 1.544368 0.8436606 2.245075
## 42 4.198520 -0.5096941 1.575195 0.8683878 2.282002
## 43 4.134507 -0.4512259 1.564455 0.8597969 2.269114
## 44 3.910132 -0.4328051 1.561072 0.8570851 2.265059
## 45 4.104486 -0.4619389 1.566423 0.8613729 2.271474
## 46 4.410975 -0.5491797 1.582448 0.8741755 2.290720
## 47 4.186417 -0.4965248 1.572776 0.8664550 2.279097
## 48 4.107593 -0.4625967 1.566544 0.8614696 2.271618
## 49 3.710886 -0.3807319 1.551507 0.8494055 2.253608
## 50 4.139040 -0.4562042 1.565370 0.8605294 2.270210
## 51 4.337959 -0.5462528 1.581910 0.8737468 2.290074
## 52 4.292496 -0.5773790 1.587628 0.8783018 2.296953
## 53 4.122338 -0.4753490 1.568886 0.8633444 2.274428
## 54 4.203322 -0.5045451 1.574249 0.8676323 2.280866
## 55 4.512315 -0.5565402 1.583800 0.8752531 2.292347
## 56 3.957226 -0.4213563 1.558969 0.8553984 2.262539
## 57 3.862381 -0.3798935 1.551353 0.8492817 2.253424
## 58 4.103151 -0.4388768 1.562187 0.8579792 2.266395
## 59 3.894105 -0.4084257 1.556594 0.8534922 2.259695
## 60 3.621001 -0.3522379 1.546273 0.8451948 2.247351
## 61 3.686992 -0.3498389 1.545832 0.8448400 2.246825
## 62 3.510224 -0.3206722 1.540475 0.8405230 2.240427
## 63 3.565779 -0.3366080 1.543402 0.8428825 2.243922
## 64 3.848933 -0.3678828 1.549147 0.8475075 2.250786
## 65 3.568410 -0.3535435 1.546513 0.8453879 2.247638
## 66 3.559730 -0.3207468 1.540489 0.8405341 2.240443
## 67 3.739270 -0.3951595 1.554157 0.8515353 2.256779
## 68 4.573360 -0.6516120 1.601263 0.8891370 2.313389
## 69 3.676290 -0.2937165 1.535524 0.8365276 2.234520
## 70 3.685993 -0.3851631 1.552321 0.8500598 2.254582
## 71 4.086000 -0.4743703 1.568707 0.8632006 2.274213
## 72 4.505385 -0.5617108 1.584750 0.8760098 2.293489
## 73 4.583815 -0.5634594 1.585071 0.8762657 2.293876
## 74 3.690767 -0.3437096 1.544707 0.8439333 2.245480
## 75 3.927508 -0.4197293 1.558670 0.8551586 2.262182
## 76 3.879901 -0.4059771 1.556144 0.8531311 2.259157
## 77 4.087428 -0.4870025 1.571027 0.8650567 2.276997
## 78 3.688273 -0.3843254 1.552167 0.8499361 2.254398
## 79 3.597398 -0.3323570 1.542621 0.8422533 2.242989
## 80 4.160061 -0.4278735 1.560166 0.8563587 2.263973
## 81 4.086790 -0.4743768 1.568708 0.8632016 2.274214
## 82 3.746937 -0.3664268 1.548879 0.8472923 2.250466
## 83 3.785830 -0.3541823 1.546630 0.8454823 2.247778
## 84 4.053035 -0.4498136 1.564196 0.8595891 2.268803
## 85 3.337257 -0.2887042 1.534603 0.8357841 2.233422
## 86 4.320307 -0.5804430 1.588190 0.8787499 2.297631
## 87 3.893212 -0.4115677 1.557171 0.8539555 2.260386
## 88 3.453204 -0.3235862 1.541010 0.8409546 2.241066
## 89 3.521160 -0.2769960 1.532452 0.8340466 2.230858
## 90 3.886727 -0.4218223 1.559055 0.8554671 2.262642
## 91 4.122523 -0.4844351 1.570555 0.8646795 2.276431
## 92 3.920514 -0.3928466 1.553732 0.8511940 2.256270
## 93 4.005262 -0.4685748 1.567642 0.8623487 2.272936
## 94 3.714949 -0.3199666 1.540345 0.8404185 2.240272
## 95 4.028866 -0.4591009 1.565902 0.8609555 2.270848
## 96 3.815141 -0.3738136 1.550236 0.8483837 2.252089
## 97 3.523394 -0.3003050 1.536734 0.8375047 2.235963
## 98 3.759121 -0.3911920 1.553428 0.8509498 2.255907
## 99 3.917464 -0.4252375 1.559682 0.8559703 2.263393
## 100 3.952978 -0.3944163 1.554020 0.8514256 2.256615
mosquito_predictions_final <- data.frame(mosquito_predictions_withbounds) %>%
mutate(X.Intercept_lwr = X.Intercept. - lwr) %>%
mutate(X.Intercept_upr = X.Intercept. + upr)
mosquito_predictions_final
## X.Intercept. dose fit lwr upr X.Intercept_lwr
## 1 4.179555 -0.5010676 1.573610 0.8671219 2.280099 3.312433
## 2 3.944414 -0.4757730 1.568964 0.8634068 2.274522 3.081007
## 3 3.346060 -0.3172197 1.539841 0.8400116 2.239670 2.506049
## 4 4.065938 -0.4743009 1.568694 0.8631904 2.274197 3.202747
## 5 3.947823 -0.3941353 1.553969 0.8513841 2.256554 3.096439
## 6 4.148586 -0.5125126 1.575713 0.8688013 2.282624 3.279785
## 7 3.972883 -0.4251264 1.559661 0.8559539 2.263369 3.116929
## 8 4.259998 -0.4934171 1.572205 0.8659987 2.278412 3.394000
## 9 4.469265 -0.5288779 1.578719 0.8712011 2.286236 3.598064
## 10 3.829872 -0.4074737 1.556419 0.8533518 2.259486 2.976520
## 11 4.334294 -0.4965038 1.572772 0.8664519 2.279092 3.467842
## 12 3.710554 -0.3820394 1.551747 0.8495986 2.253896 2.860956
## 13 3.559839 -0.3427657 1.544533 0.8437937 2.245273 2.716045
## 14 4.219150 -0.4847547 1.570614 0.8647265 2.276502 3.354424
## 15 4.009487 -0.4672119 1.567392 0.8621483 2.272635 3.147339
## 16 3.806797 -0.4031999 1.555634 0.8527215 2.258546 2.954076
## 17 3.975499 -0.3826295 1.551855 0.8496857 2.254025 3.125813
## 18 3.831982 -0.4069850 1.556329 0.8532798 2.259379 2.978702
## 19 3.789073 -0.3490203 1.545682 0.8447189 2.246645 2.944355
## 20 3.750611 -0.4024477 1.555496 0.8526105 2.258381 2.898001
## 21 3.315931 -0.2564231 1.528674 0.8309909 2.226356 2.484940
## 22 4.407929 -0.5280831 1.578573 0.8710846 2.286061 3.536844
## 23 4.377303 -0.5528935 1.583130 0.8747192 2.291541 3.502583
## 24 3.658381 -0.3697326 1.549487 0.8477808 2.251192 2.810601
## 25 4.396758 -0.5897488 1.589900 0.8801101 2.299689 3.516648
## 26 3.734986 -0.3731768 1.550119 0.8482896 2.251949 2.886697
## 27 3.703550 -0.3328067 1.542704 0.8423198 2.243088 2.861230
## 28 3.755076 -0.4171308 1.558193 0.8547756 2.261610 2.900301
## 29 3.236346 -0.2281632 1.523483 0.8267884 2.220177 2.409558
## 30 3.656518 -0.3418796 1.544370 0.8436626 2.245078 2.812855
## 31 3.781250 -0.3770837 1.550837 0.8488667 2.252807 2.932383
## 32 3.955604 -0.4288560 1.560346 0.8565034 2.264190 3.099101
## 33 4.284029 -0.5286386 1.578675 0.8711661 2.286184 3.412863
## 34 3.194729 -0.2278994 1.523434 0.8267491 2.220119 2.367980
## 35 3.020334 -0.1821160 1.515025 0.8199276 2.210122 2.200406
## 36 3.706582 -0.3738850 1.550249 0.8483943 2.252104 2.858188
## 37 3.615785 -0.3635896 1.548358 0.8468730 2.249843 2.768912
## 38 3.766586 -0.3759920 1.550636 0.8487055 2.252567 2.917880
## 39 3.953232 -0.4345951 1.561401 0.8573487 2.265453 3.095883
## 40 3.780146 -0.3272326 1.541680 0.8414946 2.241865 2.938652
## 41 3.732774 -0.3418665 1.544368 0.8436606 2.245075 2.889114
## 42 4.198520 -0.5096941 1.575195 0.8683878 2.282002 3.330132
## 43 4.134507 -0.4512259 1.564455 0.8597969 2.269114 3.274710
## 44 3.910132 -0.4328051 1.561072 0.8570851 2.265059 3.053047
## 45 4.104486 -0.4619389 1.566423 0.8613729 2.271474 3.243113
## 46 4.410975 -0.5491797 1.582448 0.8741755 2.290720 3.536800
## 47 4.186417 -0.4965248 1.572776 0.8664550 2.279097 3.319962
## 48 4.107593 -0.4625967 1.566544 0.8614696 2.271618 3.246123
## 49 3.710886 -0.3807319 1.551507 0.8494055 2.253608 2.861481
## 50 4.139040 -0.4562042 1.565370 0.8605294 2.270210 3.278511
## 51 4.337959 -0.5462528 1.581910 0.8737468 2.290074 3.464212
## 52 4.292496 -0.5773790 1.587628 0.8783018 2.296953 3.414194
## 53 4.122338 -0.4753490 1.568886 0.8633444 2.274428 3.258994
## 54 4.203322 -0.5045451 1.574249 0.8676323 2.280866 3.335689
## 55 4.512315 -0.5565402 1.583800 0.8752531 2.292347 3.637062
## 56 3.957226 -0.4213563 1.558969 0.8553984 2.262539 3.101828
## 57 3.862381 -0.3798935 1.551353 0.8492817 2.253424 3.013099
## 58 4.103151 -0.4388768 1.562187 0.8579792 2.266395 3.245172
## 59 3.894105 -0.4084257 1.556594 0.8534922 2.259695 3.040613
## 60 3.621001 -0.3522379 1.546273 0.8451948 2.247351 2.775806
## 61 3.686992 -0.3498389 1.545832 0.8448400 2.246825 2.842152
## 62 3.510224 -0.3206722 1.540475 0.8405230 2.240427 2.669701
## 63 3.565779 -0.3366080 1.543402 0.8428825 2.243922 2.722896
## 64 3.848933 -0.3678828 1.549147 0.8475075 2.250786 3.001426
## 65 3.568410 -0.3535435 1.546513 0.8453879 2.247638 2.723022
## 66 3.559730 -0.3207468 1.540489 0.8405341 2.240443 2.719195
## 67 3.739270 -0.3951595 1.554157 0.8515353 2.256779 2.887735
## 68 4.573360 -0.6516120 1.601263 0.8891370 2.313389 3.684223
## 69 3.676290 -0.2937165 1.535524 0.8365276 2.234520 2.839762
## 70 3.685993 -0.3851631 1.552321 0.8500598 2.254582 2.835933
## 71 4.086000 -0.4743703 1.568707 0.8632006 2.274213 3.222799
## 72 4.505385 -0.5617108 1.584750 0.8760098 2.293489 3.629375
## 73 4.583815 -0.5634594 1.585071 0.8762657 2.293876 3.707549
## 74 3.690767 -0.3437096 1.544707 0.8439333 2.245480 2.846834
## 75 3.927508 -0.4197293 1.558670 0.8551586 2.262182 3.072349
## 76 3.879901 -0.4059771 1.556144 0.8531311 2.259157 3.026770
## 77 4.087428 -0.4870025 1.571027 0.8650567 2.276997 3.222371
## 78 3.688273 -0.3843254 1.552167 0.8499361 2.254398 2.838337
## 79 3.597398 -0.3323570 1.542621 0.8422533 2.242989 2.755145
## 80 4.160061 -0.4278735 1.560166 0.8563587 2.263973 3.303702
## 81 4.086790 -0.4743768 1.568708 0.8632016 2.274214 3.223589
## 82 3.746937 -0.3664268 1.548879 0.8472923 2.250466 2.899645
## 83 3.785830 -0.3541823 1.546630 0.8454823 2.247778 2.940348
## 84 4.053035 -0.4498136 1.564196 0.8595891 2.268803 3.193445
## 85 3.337257 -0.2887042 1.534603 0.8357841 2.233422 2.501473
## 86 4.320307 -0.5804430 1.588190 0.8787499 2.297631 3.441558
## 87 3.893212 -0.4115677 1.557171 0.8539555 2.260386 3.039257
## 88 3.453204 -0.3235862 1.541010 0.8409546 2.241066 2.612249
## 89 3.521160 -0.2769960 1.532452 0.8340466 2.230858 2.687113
## 90 3.886727 -0.4218223 1.559055 0.8554671 2.262642 3.031260
## 91 4.122523 -0.4844351 1.570555 0.8646795 2.276431 3.257844
## 92 3.920514 -0.3928466 1.553732 0.8511940 2.256270 3.069320
## 93 4.005262 -0.4685748 1.567642 0.8623487 2.272936 3.142914
## 94 3.714949 -0.3199666 1.540345 0.8404185 2.240272 2.874531
## 95 4.028866 -0.4591009 1.565902 0.8609555 2.270848 3.167911
## 96 3.815141 -0.3738136 1.550236 0.8483837 2.252089 2.966757
## 97 3.523394 -0.3003050 1.536734 0.8375047 2.235963 2.685889
## 98 3.759121 -0.3911920 1.553428 0.8509498 2.255907 2.908171
## 99 3.917464 -0.4252375 1.559682 0.8559703 2.263393 3.061494
## 100 3.952978 -0.3944163 1.554020 0.8514256 2.256615 3.101553
## X.Intercept_upr
## 1 6.459654
## 2 6.218936
## 3 5.585730
## 4 6.340135
## 5 6.204377
## 6 6.431210
## 7 6.236252
## 8 6.538410
## 9 6.755502
## 10 6.089358
## 11 6.613387
## 12 5.964450
## 13 5.805112
## 14 6.495652
## 15 6.282123
## 16 6.065344
## 17 6.229524
## 18 6.091361
## 19 6.035719
## 20 6.008992
## 21 5.542288
## 22 6.693990
## 23 6.668843
## 24 5.909574
## 25 6.696447
## 26 5.986935
## 27 5.946638
## 28 6.016686
## 29 5.456523
## 30 5.901596
## 31 6.034056
## 32 6.219794
## 33 6.570213
## 34 5.414848
## 35 5.230456
## 36 5.958686
## 37 5.865628
## 38 6.019153
## 39 6.218684
## 40 6.022012
## 41 5.977850
## 42 6.480522
## 43 6.403621
## 44 6.175191
## 45 6.375959
## 46 6.701695
## 47 6.465514
## 48 6.379211
## 49 5.964494
## 50 6.409251
## 51 6.628032
## 52 6.589449
## 53 6.396766
## 54 6.484188
## 55 6.804662
## 56 6.219766
## 57 6.115805
## 58 6.369546
## 59 6.153801
## 60 5.868352
## 61 5.933817
## 62 5.750651
## 63 5.809700
## 64 6.099720
## 65 5.816047
## 66 5.800173
## 67 5.996049
## 68 6.886748
## 69 5.910809
## 70 5.940575
## 71 6.360213
## 72 6.798875
## 73 6.877691
## 74 5.936247
## 75 6.189689
## 76 6.139058
## 77 6.364425
## 78 5.942671
## 79 5.840387
## 80 6.424034
## 81 6.361004
## 82 5.997404
## 83 6.033608
## 84 6.321838
## 85 5.570679
## 86 6.617938
## 87 6.153599
## 88 5.694269
## 89 5.752018
## 90 6.149369
## 91 6.398954
## 92 6.176784
## 93 6.278198
## 94 5.955222
## 95 6.299715
## 96 6.067229
## 97 5.759357
## 98 6.015027
## 99 6.180858
## 100 6.209594
mosquito_plot_5c <- mosquito_plot +
geom_line(data = mosquito_log_curve,
mapping = aes(y = .fitted)) +
geom_ribbon(data = mosquito_log_curve,
mapping = aes(y = .fitted,
ymin = .lower,
ymax =.upper),
alpha = 0.5) +
geom_abline(slope = mosquito_sims$dose,
intercept = mosquito_sims$X.Intercept) +
geom_ribbon(data = mosquito_predictions_final,
mapping = aes(y = X.Intercept.,
ymin = X.Intercept_lwr,
ymax = X.Intercept_upr))
mosquito_plot_5c
### Notes: 1. I can’t figure out how to get the ribbon around the
geom_abline values 2. Close-ish, maybe! I gave it my best shot
1. I feel somewhat confident about assumption testing now
2. When we first covered it in lecture, I didn't understand what the assumptions were or how they fit into statistical analysis
3. After additional reading and practice, I get why we look at them and how they help
4. I am still a little confused on
1. Outliers makes the most sense - very straightforward
2. The positive predictor check is really cool and makes sense
3. I don't fully get homogeneity of variance, but I am getting there
1. I attempted IYb and could not get the ribbon of predicted bounds around my geom_abline
2. But going through IYa and some of IYb was helpful to understand HOW we can use simulation with a fit model
3. This would absolutely be useful - I really want to figure out how to finish this so that I can see
4. No conceptual gaps yet - just technical (code) gaps
1. This took me approximately two hours
1. I would rate myself between sufficient and strong - maybe closer to strong
2. I feel that I understand the concepts covered (mostly)
3. I was able to complete all of the problems, except IYb which I got close-ish on
4. I am definitely getting better at applying my knowledge and problem solving within R